import { createRouter, createWebHistory } from 'vue-router'
import BookCase from '../views/BookCase.vue'






const routes = [

  {
    path: '/',
    redirect: "/BookCase"
  },
  {
    path: '/BookCase',
    name: 'BookCase',
    component: BookCase
  },
  {
    path: '/BookCity',
    name: 'BookCity',
    component: () => import('../views/BookCity.vue')
  },
  {
    path: '/BookCabinet',
    name: 'BookCabinet',
    component: () => import('../views/BookCabinet.vue')
  },
  {
    path: '/BookStore',
    name: 'BookStore',
    component: () => import('../views/BookStore.vue')
  },
  {
    path: '/sign',
    name: 'sign',
    component: () => import('../views/signView.vue')
  },
  {
    path: '/bootpage',
    name: 'bootpage',
    component: () => import('../views/bootpageView.vue')
  },
  {
    path: '/Details',
    name: 'Details',
    component: () => import('../views/DetailsView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/loginView.vue')
  },
  {
    path: '/Ranking',
    name: 'Ranking',
    component: () => import('../views/RankingView.vue')
  },
  {
    path: '/Category',
    name: 'Category',
    component: () => import('../views/CategoryView.vue')
  },
  {
    path: '/Member',
    name: 'Member',
    component: () => import('../views/MemberView.vue')
  },
  {
    path: '/Search',
    name: 'Search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path: '/Recharge',
    name: 'Recharge',
    component: () => import('../views/RechargeView.vue')
  },
  {
  path: '/BookRead/:BookId',
  name: 'BookRead',
  component: () => import('../views/BookRead.vue')
},
{
path: '/End',
name: 'End',
component: () => import('../views/EndView.vue')
},
{
  path: '/author',
  name: 'author',
  component: () => import('../views/authorView.vue')
  },
  {
    path: '/mymsg',
    name: 'mymsg',
    component: () => import('../views/mymsgView.vue')
    },

]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
})
const whiteList = ['bootpage']


router.beforeEach((to, from, next) => {
  let userinfo = JSON.parse(localStorage.getItem("userinfo"));

  if (to.path === '/login') {
    return next()
  }

  if (!userinfo) {
    if (whiteList.indexOf(to.name) !== -1) {//白名单中有的路由，可以继续访问
      next()
    } else {//否则，白名单中没有的路由，跳回首页
      return next('/bootpage')
    }
  }
  next()
})



export default router
